using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace yW5Lib
{
	public class clsScenes : clsGenericBase<clsScene>
	{
		public delegate void LoadingNumEventHandler(int Scenenum);

		public delegate void SavingNumEventHandler(int Scenenum);

		public delegate void SavedEventHandler(int ID);

		private clsXml cXml;

		[CompilerGenerated]
		private LoadingNumEventHandler LoadingNumEvent;

		[CompilerGenerated]
		private SavingNumEventHandler SavingNumEvent;

		[CompilerGenerated]
		private SavedEventHandler SavedEvent;

		public event LoadingNumEventHandler LoadingNum
		{
			[CompilerGenerated]
			add
			{
				LoadingNumEventHandler loadingNumEventHandler = LoadingNumEvent;
				LoadingNumEventHandler loadingNumEventHandler2;
				do
				{
					loadingNumEventHandler2 = loadingNumEventHandler;
					LoadingNumEventHandler value2 = (LoadingNumEventHandler)Delegate.Combine(loadingNumEventHandler2, value);
					loadingNumEventHandler = Interlocked.CompareExchange(ref LoadingNumEvent, value2, loadingNumEventHandler2);
				}
				while ((object)loadingNumEventHandler != loadingNumEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				LoadingNumEventHandler loadingNumEventHandler = LoadingNumEvent;
				LoadingNumEventHandler loadingNumEventHandler2;
				do
				{
					loadingNumEventHandler2 = loadingNumEventHandler;
					LoadingNumEventHandler value2 = (LoadingNumEventHandler)Delegate.Remove(loadingNumEventHandler2, value);
					loadingNumEventHandler = Interlocked.CompareExchange(ref LoadingNumEvent, value2, loadingNumEventHandler2);
				}
				while ((object)loadingNumEventHandler != loadingNumEventHandler2);
			}
		}

		public event SavingNumEventHandler SavingNum
		{
			[CompilerGenerated]
			add
			{
				SavingNumEventHandler savingNumEventHandler = SavingNumEvent;
				SavingNumEventHandler savingNumEventHandler2;
				do
				{
					savingNumEventHandler2 = savingNumEventHandler;
					SavingNumEventHandler value2 = (SavingNumEventHandler)Delegate.Combine(savingNumEventHandler2, value);
					savingNumEventHandler = Interlocked.CompareExchange(ref SavingNumEvent, value2, savingNumEventHandler2);
				}
				while ((object)savingNumEventHandler != savingNumEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				SavingNumEventHandler savingNumEventHandler = SavingNumEvent;
				SavingNumEventHandler savingNumEventHandler2;
				do
				{
					savingNumEventHandler2 = savingNumEventHandler;
					SavingNumEventHandler value2 = (SavingNumEventHandler)Delegate.Remove(savingNumEventHandler2, value);
					savingNumEventHandler = Interlocked.CompareExchange(ref SavingNumEvent, value2, savingNumEventHandler2);
				}
				while ((object)savingNumEventHandler != savingNumEventHandler2);
			}
		}

		public event SavedEventHandler Saved
		{
			[CompilerGenerated]
			add
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Combine(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Remove(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
		}

		public clsScenes()
		{
			cXml = new clsXml();
		}

		public clsScene SceneByIndex(int Index)
		{
			return base.get_ByIndex(Index);
		}

		public clsScene SceneByID(int ID)
		{
			return base.get_ByID(ID);
		}

		public bool AddScene(ref clsScene Scene)
		{
			bool result;
			if (Added(ref Scene))
			{
				Scene.AddedToProject = true;
				result = true;
				AddHandlers(ref Scene);
			}
			else
			{
				RemoveHandlers(ref Scene);
				result = false;
			}
			return result;
		}

		public string SelectedScenesAsXML(clsCollection<int> colIDS)
		{
			string result = "";
			StringBuilder stringBuilder = new StringBuilder();
			try
			{
				stringBuilder.AppendLine("\t<SCENES>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsScene clsScene = (clsScene)enumerator.Current;
						if (colIDS.Contains(Conversions.ToString(clsScene.ID)))
						{
							stringBuilder.AppendLine(clsScene.DataAsXML(IncludingRTF: true));
						}
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				stringBuilder.AppendLine("\t</SCENES>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool WriteXML(ref StringBuilder SBP, bool EmbedRTF, bool V6File)
		{
			bool result = true;
			try
			{
				SBP.AppendLine("\t<SCENES>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					int num = default(int);
					while (enumerator.MoveNext())
					{
						clsScene clsScene = (clsScene)enumerator.Current;
						num++;
						clsScene.V6 = V6File;
						SBP.AppendLine(clsScene.DataAsXML(EmbedRTF));
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</SCENES>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void RemoveSceneByID(int ID)
		{
			if (IDUsed(ID))
			{
				clsScene Scene = SceneByID(ID);
				RemoveHandlers(ref Scene);
				RemoveByID(ID);
			}
			else
			{
				clsShared.cLog.AddAction("Tried to remove scene ID " + Conversions.ToString(ID) + " but it's not in the project.");
			}
		}

		public bool CreateFromXMLV6(string ProjectPath, string XMLData)
		{
			Clear();
			return UpdateFromXML(ProjectPath, XMLData, RTFIncluded: false, TriggerSaveEvent: false, V6File: true);
		}

		public bool CreateFromXMLV5(string ProjectPath, string XMLData)
		{
			Clear();
			return UpdateFromXML(ProjectPath, XMLData, RTFIncluded: false, TriggerSaveEvent: false, V6File: false);
		}

		public bool CreateFromXML(string Projectpath, string XMLData, bool RTFIncluded, bool TriggerSaveEvent, bool V6File)
		{
			Clear();
			return UpdateFromXML(Projectpath, XMLData, RTFIncluded, TriggerSaveEvent, V6File);
		}

		private bool UpdateFromXML(string Projectpath, string XMLData, bool RTFIncluded, bool TriggerSaveEvent, bool V6File)
		{
			bool result = true;
			try
			{
				string[] DataArray = new string[2];
				cXml.ExtractXMLArray(XMLData, "<SCENES>", "</SCENES>", "</SCENE>", ref DataArray);
				int num = DataArray.Length - 1;
				for (int i = 0; i <= num; i++)
				{
					if (Strings.InStr(1, DataArray[i], "<Scene>", CompareMethod.Text) > 0)
					{
						LoadingNumEvent?.Invoke(i + 1);
						clsScene Scene = new clsScene();
						Scene.CreateFromXML(DataArray[i]);
						if (!V6File && !RTFIncluded && !Scene.LoadRTFV5(Projectpath))
						{
							clsShared.cLog.AddAction("Unable to load RTF file " + Scene.RTFFilename);
							Interaction.MsgBox("Unable to load RTF file " + Scene.RTFFilename);
							return false;
						}
						if (!AddScene(ref Scene))
						{
							clsShared.cLog.AddAction("Unable to add scene " + Scene.Title + " to project.");
							Interaction.MsgBox("Unable to add scene " + Scene.Title + " to project.");
							return false;
						}
						if (TriggerSaveEvent)
						{
							Scene.DoCount();
							Scene.TriggerSaveEvent();
						}
					}
				}
				DataArray = null;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool SaveScenesTextV5(string ProjectPath)
		{
			bool result = true;
			try
			{
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsScene clsScene = (clsScene)enumerator.Current;
						if (!clsScene.SaveRTFV5(ProjectPath))
						{
							clsShared.cLog.AddAction("Could not save scene file " + clsScene.RTFFilename, MethodBase.GetCurrentMethod());
							result = false;
						}
					}
					return result;
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(Information.Err(), MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		private void Scene_Saved(int ID)
		{
			try
			{
				SavedEvent?.Invoke(ID);
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}

		private void RemoveHandlers(ref clsScene Scene)
		{
			try
			{
				Scene.Saved -= Scene_Saved;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}

		private void AddHandlers(ref clsScene Scene)
		{
			try
			{
				Scene.Saved += Scene_Saved;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}
	}
}
